// Copyright (c) 2007 Robin Boerdijk. All rights reserved.
//
// This software is licensed under the terms of the Microsoft Reciprocal License as described 
// in the file LICENSE.txt, which you should have received as part of this distribution. The 
// license terms are also available at http://www.opensource.org/licenses/ms-rl.html.

#include "wtkpc.h"

#include "wtk/DC.h"

namespace wtk
{
	DC::DC(HWND windowHandle, GetDCFunction getDC)
		: m_windowHandle(windowHandle)
		, m_dcHandle((*getDC)(windowHandle))
	{
	}

	DC::~DC()
	{
		win32::ReleaseDC(m_windowHandle, m_dcHandle);
	}

	SIZE DC::GetTextBoxSize(const wchar_t *text)
	{
		SIZE textBoxSize;
		textBoxSize.cx = 0;
		textBoxSize.cy = 0;
		while (true) {
			const wchar_t *eol = wcschr(text, L'\n');
			int len = (eol == NULL) ? wcslen(text) : eol - text;
			if ((len > 0) && (text[len-1] == L'\r')) {
				len--;
			}
			SIZE lineBoxSize;
			win32::GetTextExtentPoint32W(m_dcHandle, text, len, &lineBoxSize);
			if (textBoxSize.cx < lineBoxSize.cx) {
				textBoxSize.cx = lineBoxSize.cx;
			}
			textBoxSize.cy += lineBoxSize.cy;
			if (eol == NULL) {
				return textBoxSize; 
			}
			text = eol + 1;
		}
	}
}
